package com.galgram.project.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import com.galgram.project.domain.FxCourseChapter;
import com.galgram.project.domain.FxCourseCurriculumChapter;
import com.galgram.project.domain.FxCourseDetailCurriculum;
import com.galgram.project.service.IFxCourseChapterService;
import com.galgram.project.service.IFxCourseCurriculumChapterService;
import com.galgram.project.service.IFxCourseDetailCurriculumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.courseCurriculum;
import com.galgram.project.service.IcourseCurriculumService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 课程资源Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/courseCurriCulum")
public class courseCurriculumController extends BaseController {
    @Autowired
    private IcourseCurriculumService courseCurriculumService;
    @Autowired
    private IFxCourseCurriculumChapterService fxCourseCurriculumChapterService;

    /**
     * 查询课程资源列表
     */
    @PreAuthorize("@ss.hasPermi('project:courseCurriCulum:list')")
    @GetMapping("/list")
    public TableDataInfo list(courseCurriculum courseCurriculum) {
        startPage();
        List<courseCurriculum> list = courseCurriculumService.selectcourseCurriculumList(courseCurriculum);
        return getDataTable(list);
    }

    /**
     * 获取课程资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:courseCurriCulum:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(courseCurriculumService.getById(id));
    }

    /**
     * 新增课程资源
     */
    @PreAuthorize("@ss.hasPermi('project:courseCurriCulum:add')")
    @Log(title = "课程资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody courseCurriculum courseCurriculum) {
        courseCurriculumService.save(courseCurriculum);
        return success("添加成功");
    }

    /**
     * 配置资源
     */
    @PostMapping("/config")
    public AjaxResult configChapter(@RequestBody FxCourseCurriculumChapter fxCourseCurriculumChapter) {
        fxCourseCurriculumChapterService.save(fxCourseCurriculumChapter);
        return success("配置成功！");
    }


    /**
     * 修改课程资源
     */
    @PreAuthorize("@ss.hasPermi('project:courseCurriCulum:edit')")
    @Log(title = "课程资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody courseCurriculum courseCurriculum) {
        courseCurriculumService.updateById(courseCurriculum);
        return success("修改成功");
    }

    /**
     * 删除课程资源
     */
    @PreAuthorize("@ss.hasPermi('project:courseCurriCulum:remove')")
    @Log(title = "课程资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        courseCurriculumService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出课程资源列表
     */
    @PreAuthorize("@ss.hasPermi('project:courseCurriCulum:export')")
    @Log(title = "课程资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, courseCurriculum courseCurriculum) {
        List<courseCurriculum> list = courseCurriculumService.selectcourseCurriculumList(courseCurriculum);
        ExcelUtil<courseCurriculum> util = new ExcelUtil<courseCurriculum>(courseCurriculum.class);
        util.exportExcel(response, list, "课程资源数据");
    }

    /**
     * 导入课程资源列表
     */
    @PreAuthorize("@ss.hasPermi('project:courseCurriCulum:import')")
    @Log(title = "课程资源", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<courseCurriculum> util = new ExcelUtil<courseCurriculum>(courseCurriculum.class);
        List<courseCurriculum> userList = util.importExcel(file.getInputStream());
        courseCurriculumService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:courseCurriCulum:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<courseCurriculum> list = new ArrayList<courseCurriculum>();
        ExcelUtil<courseCurriculum> util = new ExcelUtil<courseCurriculum>(courseCurriculum.class);
        util.exportExcel(response, list, "课程资源数据");
    }
}
